/** 输入的是字符数组，双指针法，ES6解构赋值的特性  */
const reverseString = (s) => {
    let i = 0, j = s.length -1;
    while(i < j) {
        // [s[i], s[j]] = [s[j], s[i]];
        swap(s, i, j);
        i++;
        j--;
    }
    return s;
}

const swap = (s, i, j) => {
    let temp = s[i];
    s[i] = s[j];
    s[j] = temp;
}

const s = ['a', 'b', 'c', 'd'];
console.log(reverseString(s));

/** 输入的是字符串 递归法 */
const reverseString2 = (s) => {
    if(s.length <= 1) return s;
    return reverseString2(s.slice(1)) + s[0];
}

/** for循环 */
const reverseString3 = (s) => {
    let res = '';
    for(let i = s.length -1; i >= 0; i--)
        res += s[i];
    return res;
}

var string = "abcdefg"
console.log(reverseString2(string));
console.log(reverseString3(string));
